<?php
require('includes/application_top.php');

function remove_text($string, $text_first, $text_second)
{    
    if(strpos($string, $text_first) === false && strpos($string, $text_second) === false)
        return $string;
    $str_temp = "";
    $pattern = "|$text_first|i";
    $pattern2 = "|$text_second|i";
    preg_match($pattern , $string, $match, PREG_OFFSET_CAPTURE);
    while(sizeof($match) > 0)
    {        
        $pos_find = $match[0][1];        
        $str_temp = substr($string, 0, $pos_find);                      
                
        $string = substr($string, $pos_find + strlen($text_first), strlen($string));
        
        preg_match($pattern2, $string, $match, PREG_OFFSET_CAPTURE);
        
        if(sizeof($match) > 0)
        {
            $pos_find = $match[0][1];                        
            $string = substr($string, $pos_find + strlen($text_second), strlen($string));
            $string = $str_temp . $string;
            preg_match($pattern, $string, $match, PREG_OFFSET_CAPTURE);
        }
        
    }    
    
    return $string;
}

function remove_junk_description($products_id = 0)
{
    global $db;    
    if($products_id === 0)
        $query = $db->Execute("select products_description, products_id, language_id from " . TABLE_PRODUCTS_DESCRIPTION);
    else
        $query = $db->Execute("select products_description, products_id, language_id from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id=" . $products_id);
    while(!$query->EOF)
    {
        $id = $query->fields['products_id'];
        $lid = $query->fields['language_id'];
        $s = $query->fields['products_description'];
        $s = remove_text($s, '<h2', '>');$s = remove_text($s, '</h2', '>');
        $s = remove_text($s, '<span', '>');$s = remove_text($s, '</span', '>');
		
        $s = remove_text($s, '<font', '>');$s = remove_text($s, '</font', '>');
		$s = remove_text($s, '<h1', '>');$s = remove_text($s, '</h1', '>');
	     $s = remove_text($s, '<small', '>');$s = remove_text($s, '</small', '>');
		 $s = remove_text($s, '<big', '>');		$s = remove_text($s, '</big', '>');
        $s = remove_text($s, '<div', '>');$s = remove_text($s, '</div', '>');
        $s = remove_text($s, 'style="', '"');$s = remove_text($s, 'valign="', '"');$s = remove_text($s, 'align="', '"');
		 $s = remove_text($s, 'cellpadding="', '"');$s = remove_text($s, 'cellspacing="', '"');
		 $s = remove_text($s, 'border="', '"');
		 $s = remove_text($s, 'colspan="', '"');$s = remove_text($s, 'class="', '"');
		 $s = remove_text($s, '<strong> </strong', '>');
	
        //echo $s; exit;//for testing
        $sql = "update " . TABLE_PRODUCTS_DESCRIPTION . " set products_description=:pDescription where products_id=$id and language_id=$lid";
        $sql = $db->bindVars($sql, ':pDescription', $s, 'string');
        $db->Execute($sql);
        $query->MoveNext();
    }
}
/*
$s = '<p style="width: 900px">jadslasd</p><span style="width:200px">  fasdas </span><div style="width: 100px"> adsfasdfdssdfa </div>';
$s = remove_text($s, "<span", ">");$s = remove_text($s, "</sp", "an>");
$s = remove_text($s, "<div", ">");$s = remove_text($s, "</d", "iv>");

$s = remove_text($s, 'style="', '"');
echo ($s);
 //* 
 */
$array_indexes = array(1);
// FUNCTION TO CONVERT TRAPS - FOLLOW PRODUCT NAME + MANUFACTURER + CATEGORY
// $products_description
// $products_name, <a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $_GET['cPath'] . '&products_id=' . $_GET['products_id']) . '">' . $products_name . '</a>
// $manufacturers_name, '<a href="' . zen_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $manufacturers_id) . '">' . $manufacturers_name . '</a>'
// $categories->fields['categories_name'], '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $_GET['cPath']) . '">' . $categories_name . '</a>'
function replace_strap($find, $replace, $string, $arr_index)
{
    $str_return = "";
    $str_temp = "";
    $pattern = "|$find|i";
    $index = 1;
    preg_match($pattern , $string, $match, PREG_OFFSET_CAPTURE);
    while(sizeof($match) > 0)
    {        
        $pos_find = $match[0][1] + strlen($find);
        
        $str_temp = substr($string, 0, $pos_find);
        //echo($str_temp);exit;
        //if(in_array($index, $arr_index))
        if(strpos($str_temp, '<img ') === false)
        {
            $str_temp = preg_replace($pattern, $replace, $str_temp, 1);
        }
        $str_return .= $str_temp;
        
        $string = substr($string, $pos_find, strlen($string));
        preg_match($pattern, $string, $match, PREG_OFFSET_CAPTURE);
        $index++;
    }
    $str_return .= $string;
    
    return $str_return;
}

$s = '<img src="images/olimp/carbonox_z1.jpg"
 height="130" width="400">olimp
  <strong>Carbonox</strong> hello <strong
 >fontes</strong>';

$s = replace_strap("carbonox", "test", $s, $arr_index);
echo $s; exit;


remove_junk_description();// hard code products id here
//remove_junk_description();// hard code products id here
echo "Updated succesfully";



?>